package com.szly.phm.admin.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 导出数据工具类
 * @auther: chengjiang
 * @date: 2020-07-09 14:01
 */
public class ExportEasyExcelUtil {

    /**
     * 生成excel并下载
     * @auther: chengjiang
     * @date: 2020/9/13
     */
    public static void writeWeb(HttpServletResponse response, String fileName, List data, Class clazz) throws IOException {
        ExcelWriter excelWriter = getExcelWriter(response, fileName, clazz);
        WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
        excelWriter.write(data, writeSheet);
        excelWriter.finish();
    }

    /**
     * 生成excel的样式等等
     * @auther: chengjiang
     * @date: 2020/9/13
     */
    public static ExcelWriter getExcelWriter(HttpServletResponse response, String fileName, Class<?> clazz) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //头部样式
        WriteCellStyle headWriteCellStyle = headWriteCellStyle();
        //内容样式
        WriteCellStyle contentWriteCellStyle = contentWriteCellStyle();
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        // 获取excel的模板
//        BufferedInputStream bufferedInputStream = getExcelTemplatePath(templateName);
//        if (clazz == null) {
//            return EasyExcel.write(response.getOutputStream()).registerWriteHandler(horizontalCellStyleStrategy).withTemplate(bufferedInputStream).build();
//        } else {
//            return EasyExcel.write(response.getOutputStream(), clazz).registerWriteHandler(horizontalCellStyleStrategy).withTemplate(bufferedInputStream).build();
//        }
        if (clazz == null) {
            return EasyExcel.write(response.getOutputStream()).registerWriteHandler(horizontalCellStyleStrategy).build();
        } else {
            return EasyExcel.write(response.getOutputStream(), clazz).registerWriteHandler(horizontalCellStyleStrategy).build();
        }
    }

    /**
     * 头部样式
     * @auther: chengjiang
     * @date: 2020/9/13
     */
    private static WriteCellStyle headWriteCellStyle() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);
        return headWriteCellStyle;
    }

    /**
     * 内容样式
     * @auther: chengjiang
     * @date: 2020/9/13
     */
    private static WriteCellStyle contentWriteCellStyle() {
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        WriteFont contenWriteFont = new WriteFont();
        contenWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contenWriteFont);
        return contentWriteCellStyle;
    }

    /**
     * 获取excel的模板并转化成流
     * @auther: chengjiang
     * @date: 2020/9/13
     */
//    public static BufferedInputStream getExcelTemplatePath(String templateName) throws IOException {
//        org.springframework.core.io.ClassPathResource cpr = new org.springframework.core.io.ClassPathResource("/template/excel/" + templateName);
//        return new BufferedInputStream(cpr.getInputStream());
//    }

}
